package Form;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;

import javax.swing.ComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import Control.CommonControls;
import Control.Job_View_Controller;
import Entity.Job_Registration;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.Color;
import java.awt.FlowLayout;

@SuppressWarnings("serial")
public class Job_SearchForm extends JPanel {
	private JDialog updatePanel;
	private JLabel lblSearchJob;
	private JLabel lblJobName;
	private JTextField searchJobName;
	private JButton btnSearchName;
	private JComboBox searchJobReq;
	private JLabel lblJobRequirements;
	private JButton btnSearchReq;
	private JLabel lblJobCategory;
	private JComboBox searchJobCat;
	
	public JFrame getParser() {
		return parser;
	}
	public void setParser(JFrame parser) {
		this.parser = parser;
	}
	public Job_View_Controller getControl() {
		return control;
	}
	public void setControl(Job_View_Controller control) {
		this.control = control;
	}
	
	private JButton btnSearchCat;
	private JScrollPane scrollPane;
	private JPanel panel; 
	
	private JFrame parser;
	private Job_View_Controller control;
	private JButton btnRegisterForJob;
	private JPanel headerPanel;
	private JLabel lblJobId;
	private JLabel lblJobName_1;
	private JLabel lblDescription;
	private JLabel lblEducationRequired;
	private JLabel lblNewLabel;
	private JLabel lblSlot;
	private JLabel lblNewLabel_1;
	private JLabel lblAvailability;
	
	public Job_SearchForm(JFrame frame) {
		this.setParser(frame);
		this.setControl(new Job_View_Controller(this));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {30, 100, 220, 100, 30};
		gridBagLayout.rowHeights = new int[]{0, 0, 165, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		GridBagConstraints gbc_lblSearchJob = new GridBagConstraints();
		gbc_lblSearchJob.insets = new Insets(0, 0, 5, 5);
		gbc_lblSearchJob.gridx = 2;
		gbc_lblSearchJob.gridy = 0;
		add(getLblSearchJob(), gbc_lblSearchJob);
		GridBagConstraints gbc_btnRegisterForJob = new GridBagConstraints();
		gbc_btnRegisterForJob.insets = new Insets(0, 0, 5, 5);
		gbc_btnRegisterForJob.gridx = 1;
		gbc_btnRegisterForJob.gridy = 1;
		add(getBtnRegisterForJob(), gbc_btnRegisterForJob);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 2;
		add(getScrollPane(), gbc_scrollPane);
		GridBagConstraints gbc_lblJobName = new GridBagConstraints();
		gbc_lblJobName.anchor = GridBagConstraints.EAST;
		gbc_lblJobName.insets = new Insets(0, 0, 5, 5);
		gbc_lblJobName.gridx = 1;
		gbc_lblJobName.gridy = 4;
		add(getLblJobName(), gbc_lblJobName);
		GridBagConstraints gbc_searchJobName = new GridBagConstraints();
		gbc_searchJobName.insets = new Insets(0, 0, 5, 5);
		gbc_searchJobName.fill = GridBagConstraints.HORIZONTAL;
		gbc_searchJobName.gridx = 2;
		gbc_searchJobName.gridy = 4;
		add(getSearchJobName(), gbc_searchJobName);
		GridBagConstraints gbc_btnSearchName = new GridBagConstraints();
		gbc_btnSearchName.insets = new Insets(0, 0, 5, 5);
		gbc_btnSearchName.gridx = 2;
		gbc_btnSearchName.gridy = 5;
		add(getBtnSearchName(), gbc_btnSearchName);
		GridBagConstraints gbc_lblJobRequirements = new GridBagConstraints();
		gbc_lblJobRequirements.insets = new Insets(0, 0, 5, 5);
		gbc_lblJobRequirements.anchor = GridBagConstraints.EAST;
		gbc_lblJobRequirements.gridx = 1;
		gbc_lblJobRequirements.gridy = 6;
		add(getLblJobRequirements(), gbc_lblJobRequirements);
		GridBagConstraints gbc_searchJobReq = new GridBagConstraints();
		gbc_searchJobReq.insets = new Insets(0, 0, 5, 5);
		gbc_searchJobReq.fill = GridBagConstraints.HORIZONTAL;
		gbc_searchJobReq.gridx = 2;
		gbc_searchJobReq.gridy = 6;
		add(getSearchJobReq(), gbc_searchJobReq);
		GridBagConstraints gbc_btnSearchReq = new GridBagConstraints();
		gbc_btnSearchReq.insets = new Insets(0, 0, 5, 5);
		gbc_btnSearchReq.gridx = 2;
		gbc_btnSearchReq.gridy = 7;
		add(getBtnSearchReq(), gbc_btnSearchReq);
		GridBagConstraints gbc_lblJobCategory = new GridBagConstraints();
		gbc_lblJobCategory.anchor = GridBagConstraints.EAST;
		gbc_lblJobCategory.insets = new Insets(0, 0, 5, 5);
		gbc_lblJobCategory.gridx = 1;
		gbc_lblJobCategory.gridy = 8;
		add(getLblJobCategory(), gbc_lblJobCategory);
		GridBagConstraints gbc_searchJobCat = new GridBagConstraints();
		gbc_searchJobCat.insets = new Insets(0, 0, 5, 5);
		gbc_searchJobCat.fill = GridBagConstraints.HORIZONTAL;
		gbc_searchJobCat.gridx = 2;
		gbc_searchJobCat.gridy = 8;
		add(getSearchJobCat(), gbc_searchJobCat);
		GridBagConstraints gbc_btnSearchCat = new GridBagConstraints();
		gbc_btnSearchCat.insets = new Insets(0, 0, 5, 5);
		gbc_btnSearchCat.gridx = 2;
		gbc_btnSearchCat.gridy = 9;
		add(getBtnSearchCat(), gbc_btnSearchCat);
	}
	public JLabel getLblSearchJob() {
		if (lblSearchJob == null) {
			lblSearchJob = new JLabel("Search Job");
			lblSearchJob.setFont(new Font("Tahoma", Font.PLAIN, 20));
		}
		return lblSearchJob;
	}
	public JLabel getLblJobName() {
		if (lblJobName == null) {
			lblJobName = new JLabel("Job Name:");
			lblJobName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		}
		return lblJobName;
	}
	public JTextField getSearchJobName() {
		if (searchJobName == null) {
			searchJobName = new JTextField();
			searchJobName.setColumns(10);
		}
		return searchJobName;
	}
	public JButton getBtnSearchName() {
		if (btnSearchName == null) {
			btnSearchName = new JButton("Search by Name"); 
			final Job_SearchForm panel1 = this;
			btnSearchName.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) { 
					if (CommonControls.validatePanelFields(panel1) == true){
					control.searchJobName(); 
					}
				}
			});
			btnSearchName.setFont(new Font("Tahoma", Font.BOLD, 15));
		}
		return btnSearchName;
	}
	public JComboBox getSearchJobReq() {
		if (searchJobReq == null) {
			searchJobReq = new JComboBox();
			searchJobReq.setFont(new Font("Tahoma", Font.PLAIN, 15));
			searchJobReq.setModel(new DefaultComboBoxModel(new String[] {"PSLE", "N Levels ", "O Levels ", "Tertiary Diploma ", "Masters ", "PHD"}));
		}
		return searchJobReq;
	}
	public JLabel getLblJobRequirements() {
		if (lblJobRequirements == null) {
			lblJobRequirements = new JLabel("Job Requirements:");
			lblJobRequirements.setFont(new Font("Tahoma", Font.PLAIN, 15));
		}
		return lblJobRequirements;
	}
	public JButton getBtnSearchReq() {
		if (btnSearchReq == null) {
			btnSearchReq = new JButton("Search by Requirements"); 

			btnSearchReq.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {  
					control.searchJobReq();
				}
			});
			btnSearchReq.setFont(new Font("Tahoma", Font.BOLD, 15));
		}
		return btnSearchReq;
	}
	public JLabel getLblJobCategory() {
		if (lblJobCategory == null) {
			lblJobCategory = new JLabel("Job Category:");
			lblJobCategory.setFont(new Font("Tahoma", Font.PLAIN, 15));
		}
		return lblJobCategory;
	}
	public JComboBox getSearchJobCat() {
		if (searchJobCat == null) {
			searchJobCat = new JComboBox();
			searchJobCat.setFont(new Font("Tahoma", Font.PLAIN, 15));
			searchJobCat.setModel(new DefaultComboBoxModel(new String[] {"Accounting ", "Admin & Clerical ", "Automotive ", "Banking ", "Biotech ", "Broadcast - Journalism ", "Business Development ", "Construction ", "Consultant ", "Customer Service ", "Design ", "Distribution - Shipping ", "Education - Teaching ", "Engineering ", "Entry Level - New Grad ", "Executive ", "Facilities ", "Finance ", "Franchise", "General Business ", "General Labor ", "Government ", "Grocery ", "Health Care ", "Hotel - Hospitality ", "Human Resources ", "Information Technology ", "Installation - Maint - Repair ", "Insurance ", "Inventory ", "Legal ", "Legal Admin ", "Management ", "Manufacturing ", "Marketing ", "Media - Journalism - Newspaper ", "Nonprofit - Social Services  ", "Nurse", "Pharmaceutical ", "Professional Services ", "Purchasing - Procurement ", "QA - Quality Control ", "Real Estate ", "Research ", "Restaturant - Food Service ", "Retail ", "Sales ", "Science ", "Skilled Labor - Trades ", "Strategy - Planning ", "Supply CHain ", "Telecommunications ", "Training", "Transportation ", "Warehouse"}));
		}
		return searchJobCat;
	}
	public JButton getBtnSearchCat() {
		if (btnSearchCat == null) {
			btnSearchCat = new JButton("Search by Category");
			btnSearchCat.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) { 
					String catCode = (String) getSearchJobCat().getSelectedItem(); 
					Job_Registration.searchJobCat(catCode);
					
				}
			});
			btnSearchCat.setFont(new Font("Tahoma", Font.BOLD, 15));
		}
		return btnSearchCat;
	}
	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getPanel());
			scrollPane.setPreferredSize(new Dimension(440, 165));
			scrollPane.setColumnHeaderView(getHeaderPanel());
		}
		return scrollPane;
	}
	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setPreferredSize(new Dimension(420, 165));
		}
		return panel;
	}
	public JButton getBtnRegisterForJob() {
		if (btnRegisterForJob == null) {
			btnRegisterForJob = new JButton("Update Job");
			btnRegisterForJob.setFont(new Font("Tahoma", Font.BOLD, 15)); 
			if (CommonControls.getUserType() == "Staff"){
				btnRegisterForJob.setVisible(true);
			}
			else{
				btnRegisterForJob.setVisible(false);
			}
			
			btnRegisterForJob.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					updatePanel = new JDialog(parser, true);
					updatePanel.getContentPane().add(new Job_UpdateForm(parser));
					
					updatePanel.pack();
					updatePanel.setLocationRelativeTo(parser);
					updatePanel.setVisible(true);
				}
			});
			
		}
		return btnRegisterForJob; 
	}
	
	public JPanel getHeaderPanel() {
		if (headerPanel == null) {
			headerPanel = new JPanel();
			FlowLayout flowLayout = (FlowLayout) headerPanel.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			headerPanel.setBackground(Color.LIGHT_GRAY);
			headerPanel.add(getLblJobId());
			headerPanel.add(getLblJobName_1());
			headerPanel.add(getLblDescription());
			headerPanel.add(getLblEducationRequired());
			headerPanel.add(getLblNewLabel());
			headerPanel.add(getLblSlot());
			headerPanel.add(getLblNewLabel_1());
			headerPanel.add(getLblAvailability());
		}
		return headerPanel;
	}
	public JLabel getLblJobId() {
		if (lblJobId == null) {
			lblJobId = new JLabel("Job ID");
			lblJobId.setBackground(Color.BLACK);
			lblJobId.setPreferredSize(new Dimension (110, 20)); 
			lblJobId.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblJobId;
	}
	public JLabel getLblJobName_1() {
		if (lblJobName_1 == null) {
			lblJobName_1 = new JLabel("Job Name");
			lblJobName_1.setBackground(Color.LIGHT_GRAY); 
			lblJobName_1.setPreferredSize(new Dimension (100, 20)); 
			lblJobName_1.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblJobName_1;
	}
	public JLabel getLblDescription() {
		if (lblDescription == null) {
			lblDescription = new JLabel("Description"); 
			lblDescription.setPreferredSize(new Dimension (120, 20)); 
			lblDescription.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblDescription;
	}
	public JLabel getLblEducationRequired() {
		if (lblEducationRequired == null) {
			lblEducationRequired = new JLabel("Education Required"); 
			lblEducationRequired.setPreferredSize(new Dimension (120, 20)); 
			lblEducationRequired.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblEducationRequired;
	}
	public JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Salary"); 
			lblNewLabel.setPreferredSize(new Dimension (50, 20)); 
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel;
	}
	public JLabel getLblSlot() {
		if (lblSlot == null) {
			lblSlot = new JLabel("Slot"); 
			lblSlot.setPreferredSize(new Dimension (50, 20)); 
			lblSlot.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblSlot;
	}
	public JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("Category"); 
			lblNewLabel_1.setPreferredSize(new Dimension (170, 20));
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER); 
		}
		return lblNewLabel_1;
	}
	public JLabel getLblAvailability() {
		if (lblAvailability == null) {
			lblAvailability = new JLabel("Availability"); 
			lblAvailability.setPreferredSize(new Dimension (100, 20)); 
			lblAvailability.setHorizontalAlignment(SwingConstants.CENTER); 
		}
		return lblAvailability;
	}
}